package org.celery.modules.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.celery.modules.user.entity.UserDomain;
import org.celery.modules.user.vo.UserDomainVO;
import org.celery.modules.user.wrapper.UserDomainWrapper;
import org.celery.modules.user.service.IUserDomainService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户与域名关联表 控制器
 *
 * @author Celery
 * @since 2022-04-06
 */
@RestController
@AllArgsConstructor
@RequestMapping("celery-user/user-domain")
@Api(value = "用户与域名关联表", tags = "用户与域名关联表接口")
public class UserDomainController extends BladeController {

	private final IUserDomainService userDomainService;
	private final IUserService userService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入userDomain")
	public R<UserDomainVO> detail(UserDomain userDomain) {
		UserDomain detail = userDomainService.getOne(Condition.getQueryWrapper(userDomain));
		return R.data(UserDomainWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 用户与域名关联表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入userDomain")
	public R<IPage<UserDomainVO>> list(UserDomain userDomain, Query query, BladeUser bladeUser) {
		QueryWrapper<UserDomain> queryWrapper = Condition.getQueryWrapper(userDomain);
		if (!SecureUtil.isAdministrator()) {
			queryWrapper.lambda().eq(UserDomain::getTenantId, bladeUser.getTenantId());
			queryWrapper.lambda().in(UserDomain::getUserId, userService.getUnderUserIdList(bladeUser).stream().map(User::getId).collect(Collectors.toList()));
		}
		IPage<UserDomain> pages = userDomainService.page(Condition.getPage(query), queryWrapper);
		return R.data(UserDomainWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 用户与域名关联表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入userDomain")
	public R<IPage<UserDomainVO>> page(UserDomainVO userDomain, Query query) {
		IPage<UserDomainVO> pages = userDomainService.selectUserDomainPage(Condition.getPage(query), userDomain);
		return R.data(pages);
	}

	/**
	 * 新增 用户与域名关联表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入userDomain")
	public R save(@Valid @RequestBody UserDomain userDomain) {
		return R.status(userDomainService.save(userDomain));
	}

	/**
	 * 修改 用户与域名关联表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入userDomain")
	public R update(@Valid @RequestBody UserDomain userDomain) {
		return R.status(userDomainService.updateById(userDomain));
	}

	/**
	 * 新增或修改 用户与域名关联表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入userDomain")
	public R submit(@Valid @RequestBody UserDomain userDomain, BladeUser bladeUser) {
		if (Func.isEmpty(userDomain.getId())) {
			userDomain.setTenantId(bladeUser.getTenantId());
		}

		List<UserDomain> userDomainList = userDomainService.list(Wrappers.<UserDomain>lambdaQuery()
				.eq(UserDomain::getDomain, userDomain.getDomain())
		);
		for (UserDomain item : userDomainList) {
			if (!item.getId().equals(userDomain.getId()) && userDomain.getDomain().equals(item.getDomain())) {
				throw new ServiceException("域名已存在!");
			}
		}

		return R.status(userDomainService.saveOrUpdate(userDomain));
	}

	
	/**
	 * 删除 用户与域名关联表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(userDomainService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 选项 用户表.
	 */
	@GetMapping("/select")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "选项")
	public R<List<User>> select(User user, BladeUser bladeUser) {
		QueryWrapper<User> queryWrapper = Condition.getQueryWrapper(user);

		if (!SecureUtil.isAdministrator()) {
			queryWrapper.lambda().eq(User::getTenantId, bladeUser.getTenantId());
			queryWrapper.lambda().in(User::getId, userService.getUnderUserIdList(bladeUser).stream().map(User::getId).collect(Collectors.toList()));
		}

		return R.data(userService.list(queryWrapper));
	}
}
